home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / switchproxy.xpi / chrome / switchproxy.jar / content / dialogs / editanon.js < prev    next >
Encoding:
Text File  |  2006-04-11  |  16.5 KB  |  495 lines

  1.  
  2. var gProxy        = null;
  3. var gProxyList    = null;
  4. var gProxyLabel    = "";
  5. var gAutoUrl     = false;
  6. var gIsEdit        = false;
  7. var gAutoTime    = 0;
  8.  
  9. // Prepopulate fields
  10. function switchproxy_anon_initDialog(){
  11.     
  12.     gProxyList    = document.getElementById('proxy-list');
  13.     gProxyLabel = document.getElementById('proxy-label-field');
  14.     
  15.     if(window.arguments.length > 1 && window.arguments[0] == "edit" && !switchproxy_isEmpty(window.arguments[1])){
  16.         
  17.         gIsEdit        = true;
  18.         gProxyURI     = window.arguments[1];        
  19.         
  20.         try{
  21.             gProxy = switchProxy_ds_getResource(gProxyURI);
  22.         
  23.             //Load Fields
  24.                 var oProxyConfig = switchProxy_ds_getPropertyValuesFor(gProxyURI);
  25.                 
  26.                 // Label
  27.                 gProxyLabel.value = oProxyConfig[gSProxyRdfNodeName];
  28.                                 
  29.                 // Auto URL & Checkbox
  30.                 if(typeof(oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdate"]) != 'undefined' && oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdate"] == "true"){
  31.                     document.getElementById('auto-import-check').checked = true;
  32.                     switchproxy_anon_enableAutoUrl(document.getElementById('auto-import-check'))
  33.                                         
  34.                     // URL
  35.                     if(typeof(oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdateUrl"]) != 'undefined');
  36.                         document.getElementById('import-url-field').value = oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdateUrl"];
  37.                 }
  38.                 
  39.                 // Last Auto Update
  40.                 if(typeof(oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdateTime"]) != 'undefined' && !isNaN(oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdateTime"]))
  41.                     gAutoTime = parseInt(oProxyConfig[gSProxyRdfNodeUriRoot + "#autoUpdateTime"]);
  42.                 
  43.                 // Interval
  44.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#interval"])
  45.                     document.getElementById('increment-field').value = oProxyConfig[gSProxyRdfNodeUriRoot + "#interval"];
  46.                 else
  47.                     document.getElementById('increment-field').value = "60";
  48.                 
  49.                 // Proxy List
  50.                 var aProxyList = switchProxy_ds_getValuesFor(gProxy, switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#proxy"));
  51.                 for(var i = 0; i < aProxyList.length; i++){
  52.                     switchproxy_anon_addProxyToList(aProxyList[i]);
  53.                 }
  54.                 
  55.                 // Proxy Types
  56.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#http"])
  57.                     document.getElementById("type_http").setAttribute("checked", oProxyConfig[gSProxyRdfNodeUriRoot + "#http"]);
  58.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#ssl"])
  59.                     document.getElementById("type_ssl").setAttribute("checked", oProxyConfig[gSProxyRdfNodeUriRoot + "#ssl"]);
  60.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#ftp"])
  61.                     document.getElementById("type_ftp").setAttribute("checked", oProxyConfig[gSProxyRdfNodeUriRoot + "#ftp"]);
  62.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#gopher"])
  63.                     document.getElementById("type_gopher").setAttribute("checked", oProxyConfig[gSProxyRdfNodeUriRoot + "#gopher"]);
  64.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#socks"])
  65.                     document.getElementById("type_socks").setAttribute("checked", oProxyConfig[gSProxyRdfNodeUriRoot + "#socks"]);
  66.                     
  67.                 if(oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"] && oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"] == "4"){
  68.                     document.getElementById("socks_v4").setAttribute("selected", "true");
  69.                     document.getElementById("socks_v5").setAttribute("selected", "false");
  70.                 }
  71.                 else if(oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"] && oProxyConfig[gSProxyRdfNodeUriRoot + "#socks_type"] == "5"){
  72.                     document.getElementById("socks_v4").setAttribute("selected", "false");
  73.                     document.getElementById("socks_v5").setAttribute("selected", "true");
  74.                 }
  75.                     
  76.                         
  77.         }catch(err){ alert(switchproxy_getString("error.unknown") +"\n"+ err); self.close(); }
  78.     }
  79.   
  80.   // Thunderbird
  81.   if(navigator.userAgent.search(/Thunderbird/gi) > -1){
  82.       document.getElementById("type_ftp").style.display = "none";
  83.       document.getElementById("type_gopher").style.display = "none";
  84.   }
  85.     
  86. }
  87.  
  88. // Open file picker
  89. //    set sId value to file path
  90. //    on return OK, enable sEnableId element
  91. function switchproxy_anon_pickFile(sId, sEnableId){
  92.     
  93.     try{
  94.         //Open Dialog
  95.         var oField            = document.getElementById(sId);
  96.         var nsIFilePicker    = Components.interfaces.nsIFilePicker;
  97.         var oPicker            = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  98.             oPicker.init(window, "Select a File", nsIFilePicker.modeOpen);
  99.             oPicker.appendFilter("Text Files","*.txt");
  100.             oPicker.appendFilter("Document Files","*.doc");
  101.             oPicker.appendFilter("All Files","*.*");
  102.         var oRes            = oPicker.show();
  103.         
  104.         //Get Filepath
  105.         if(oRes == nsIFilePicker.returnOK && oField != null){
  106.             oField.value = oPicker.file.path;
  107.             
  108.             if(sEnableId != null){
  109.                 oEnable = document.getElementById(sEnableId);
  110.                 
  111.                 if(oEnable)
  112.                     oEnable.disabled = false;
  113.             }    
  114.         }
  115.     }catch(err){ alert(switchproxy_getString("error.unknown") +"\n"+ err); }
  116. }
  117.  
  118. // Manuall add proxy
  119. function switchproxy_anon_addProxy(){
  120.     
  121.     var oAddField = document.getElementById('add-field');
  122.     var oAddButton = document.getElementById('add-button');
  123.     var oProxyList = document.getElementById('proxy-list');
  124.     
  125.     //Validate
  126.         //Empty
  127.         if(switchproxy_isEmpty(oAddField.value)){
  128.             alert(switchproxy_getString("add.anon.validate.empty"));
  129.             return;
  130.         }
  131.         //Does entry exist?
  132.         if(switchproxy_existsInList(oProxyList, oAddField.value)){
  133.             alert(switchproxy_getString("add.anon.validate.duplicate"));
  134.             return;
  135.         }
  136.         //Is valid Domain or IP
  137.         if(!switchproxy_isValidDomain(oAddField.value)){
  138.             alert(switchproxy_getString("add.anon.validate.domain"));
  139.             return;
  140.         }
  141.     
  142.     //Add Item
  143.     switchproxy_anon_addProxyToList(oAddField.value);
  144.     
  145.     //Clear field
  146.     oAddField.value = "";
  147. }
  148.  
  149. // Manually remove proxy
  150. function switchproxy_anon_removeProxy(){
  151.     
  152.     var oProxyList    = document.getElementById('proxy-list');
  153.     var aSelected    = oProxyList.selectedItems;
  154.     var iSelected    = oProxyList.getIndexOfItem(aSelected[0]);
  155.     
  156.     if(oProxyList.selectedItem == null){
  157.         alert(switchproxy_getString("add.anon.validate.remove"));
  158.         return;
  159.     }
  160.     
  161.     //Remove Selected
  162.     for(var i = (oProxyList.selectedCount - 1); i < oProxyList.selectedCount; i--){
  163.         oProxyList.removeItemAt(oProxyList.getIndexOfItem(aSelected[i]));
  164.     }
  165.     
  166.     //Select next item
  167.     if(oProxyList.getRowCount() > 0){
  168.         if(oProxyList.getRowCount() >= (iSelected + 1)){
  169.             oProxyList.selectItem(oProxyList.getItemAtIndex(iSelected));
  170.         }
  171.         else if(oProxyList.getRowCount() > 0){
  172.             oProxyList.selectItem(oProxyList.getItemAtIndex(iSelected - 1));
  173.         }
  174.         
  175.         oProxyList.focus();
  176.     }
  177. }
  178.  
  179. /*
  180. * Save Anon Configuration
  181. */
  182. function switchproxy_anon_save(){
  183.     
  184.     var oProxyLabel        = document.getElementById("proxy-label-field");
  185.     var oUrlField        = document.getElementById("import-url-field");
  186.     var oUrlAutoCheck    = document.getElementById("auto-import-check");
  187.     var oIntervalField    = document.getElementById("increment-field");
  188.     var oProxyList        = document.getElementById("proxy-list");
  189.     
  190.     //Validation
  191.         //Empty Label
  192.         if(switchproxy_isEmpty(oProxyLabel.value)){
  193.             alert(switchproxy_getString("add.anon.validate.label"));
  194.             oProxyLabel.focus();
  195.             return false;
  196.         }
  197.         //Label with special chars
  198.         if(!switchproxy_allowedChars(oProxyLabel.value)){
  199.             alert(switchproxy_getString("error.add.invalid"));
  200.             return false;
  201.         }
  202.         //Label can't be named 'None'
  203.         if(switchproxy_simplify(oProxyLabel.value) == switchproxy_simplify(switchproxy_getString("common.proxy.none"))){
  204.             alert(switchproxy_getString("error.add.duplicate"));
  205.             return false;
  206.         }
  207.         //Is This a Duplicate Label?
  208.         var oTestProxy = switchProxy_ds_getElementForValue(gSProxyRdfNodeName, oProxyLabel.value);
  209.         if(oTestProxy != null && (!gIsEdit    || oTestProxy.Value != gProxy.Value)){
  210.             alert(switchproxy_getString("error.add.duplicate"));
  211.             return false;
  212.         }
  213.         //Empty Interval
  214.         if(switchproxy_isEmpty(oIntervalField.value)){
  215.             alert(switchproxy_getString("add.anon.validate.rotation"));
  216.             oIntervalField.focus();
  217.             return false;
  218.         }
  219.         //Interval not a number
  220.         if(isNaN(oIntervalField.value)){
  221.             alert(switchproxy_getString("add.anon.validate.rotation.type"));
  222.             oIntervalField.focus();
  223.             return false;
  224.         }
  225.         
  226.     try{
  227.         var oProxy        = null;
  228.         var sProxyUri     = opener.switchproxy_getUniqueProxyUri();
  229.         
  230.         /*
  231.         * EDIT
  232.         */
  233.             if(gIsEdit){
  234.                 oProxy = gProxy;
  235.             
  236.                 //Remove Properties
  237.                     //Label
  238.                     switchProxy_ds_removeProperty(gSProxyRdfNodeName, oProxy);
  239.                     //Interval
  240.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#interval", oProxy);
  241.                     //Auto Update
  242.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#autoUpdate", oProxy);
  243.                     //Auto Update URL
  244.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#autoUpdateUrl", oProxy);
  245.                     //Proxy
  246.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#proxy", oProxy);
  247.                     
  248.                     // Types
  249.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#http", oProxy);
  250.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#ssl", oProxy);
  251.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#ftp", oProxy);
  252.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#gopher", oProxy);
  253.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#http", oProxy);
  254.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#socks", oProxy);
  255.                     switchProxy_ds_removeProperty(gSProxyRdfNodeUriRoot + "#socks_type", oProxy);
  256.             }        
  257.         
  258.         /*
  259.         * ADD
  260.         */
  261.             //Create New Element
  262.             if(!gIsEdit)
  263.                 var oProxy = switchProxy_ds_getResource(sProxyUri);
  264.             
  265.             //Add Element
  266.             switchProxy_ds_addElement(sProxyUri);
  267.             
  268.             //Add Properties
  269.                 // Proxy Label
  270.                 switchProxy_ds_addProperty(oProxy, 
  271.                                                 switchProxy_ds_getResource(gSProxyRdfNodeName), 
  272.                                                 oProxyLabel.value, true);
  273.                 // Type = 3 (anon)
  274.                 switchProxy_ds_addProperty(oProxy, 
  275.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#networkProxyType"), 
  276.                                             "3", true);
  277.                 // Interval    
  278.                 switchProxy_ds_addProperty(oProxy, 
  279.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#interval"), 
  280.                                             oIntervalField.value, true);
  281.                 // Auto Update
  282.                 switchProxy_ds_addProperty(oProxy, 
  283.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdate"), 
  284.                                             oUrlAutoCheck.checked, true);
  285.                 // Auto Update URL
  286.                 if(oUrlAutoCheck.checked){
  287.                     switchProxy_ds_addProperty(oProxy, 
  288.                                                 switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdateUrl"), 
  289.                                                 oUrlField.value, true);
  290.                 }
  291.                 
  292.                 //Last Auto Update
  293.                 switchProxy_ds_addProperty(oProxy, 
  294.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#autoUpdateTime"), 
  295.                                             gAutoTime, true);                
  296.                 
  297.                 // Types
  298.                 switchProxy_ds_addProperty(oProxy, 
  299.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#http"), 
  300.                                             document.getElementById("type_http").checked, true);    
  301.                 switchProxy_ds_addProperty(oProxy, 
  302.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#ssl"), 
  303.                                             document.getElementById("type_ssl").checked, true);    
  304.                 switchProxy_ds_addProperty(oProxy, 
  305.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#ftp"), 
  306.                                             document.getElementById("type_ftp").checked, true);    
  307.                 switchProxy_ds_addProperty(oProxy, 
  308.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#gopher"), 
  309.                                             document.getElementById("type_gopher").checked, true);    
  310.                 switchProxy_ds_addProperty(oProxy, 
  311.                                             switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#socks"), 
  312.                                             document.getElementById("type_socks").checked, true);
  313.                 
  314.                 if(document.getElementById("socks_v4").getAttribute("selected") == "true"){
  315.                     switchProxy_ds_addProperty(oProxy, 
  316.                                                 switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#socks_type"), 
  317.                                                 "4", true);
  318.                 }
  319.                 else if(document.getElementById("socks_v5").getAttribute("selected") == "true"){
  320.                     switchProxy_ds_addProperty(oProxy, 
  321.                                                 switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#socks_type"), 
  322.                                                 "5", true);
  323.                 }
  324.                 
  325.                 //Proxy List
  326.                 var sProxy = "";
  327.                 for(var i = 0; i < oProxyList.getRowCount(); i++){
  328.                     sProxy = oProxyList.getItemAtIndex(i).getAttribute("value");
  329.                     if(sProxy && !switchproxy_isEmpty(sProxy)){
  330.                         switchProxy_ds_addProperty(oProxy, 
  331.                                                     switchProxy_ds_getResource(gSProxyRdfNodeUriRoot + "#proxy"), 
  332.                                                     sProxy, true);
  333.                     }
  334.                 }
  335.                 
  336.         //Refresh Browser List
  337.         var oPrefs    = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  338.             oPrefs.setIntPref("switchproxy.proxy.rdf.lastupdate", (new Date()).getTime());
  339.     
  340.         //Set Auto Update
  341.         if(opener.switchproxy_anon_getAutoUpdateTimout() == null)
  342.             gAnonUpdateTimout = setTimeout("switchproxy_anon_autoUpdateLists()", 600000);
  343.                 
  344.         switchproxy_openerFocus();
  345.     
  346.     }catch(err){ alert(switchproxy_getString("error.unknown") +"\n("+ err +")"); return false; }
  347. }
  348.  
  349. /*
  350. * IMPORT WRAPPERS
  351. */
  352.     // File
  353.     function switchproxy_anon_importFile(){
  354.         var oField    = document.getElementById("import-file-field");
  355.         var oButton    = document.getElementById("import-file-button");
  356.         var oList    = document.getElementById("proxy-list");
  357.         var sValue    = oField.value;
  358.         
  359.         try{
  360.             
  361.             oButton.blur();
  362.             
  363.             //Cancel
  364.             if(oButton.label == "Cancel"){
  365.                 switchproxy_anon_cancelImport();
  366.                 oButton.label = switchproxy_getString("common.buttons.load");
  367.                 return;
  368.             }
  369.             
  370.             //Load
  371.             if(!switchproxy_isEmpty(sValue)){
  372.                 oButton.label    = switchproxy_getString("common.buttons.cancel");
  373.                 oField.value    = switchproxy_getString("toolbar.loading");
  374.                 switchproxy_anon_import("file", sValue, oList, null);
  375.                 oField.value     = sValue;
  376.                 oButton.label    = switchproxy_getString("common.buttons.load");
  377.                 
  378.                 if(switchproxy_anon_getNumImported() == 0)
  379.                     alert(switchproxy_getString("add.anon.import.file.validate.empty"));
  380.                 else if(switchproxy_anon_getNewImported() == 0)
  381.                     alert(switchproxy_getString("add.anon.import.validate.old"));
  382.             }
  383.             
  384.                 
  385.         }catch(err){ alert(switchproxy_getString("error.unknown") +"\n("+ err +")"); }
  386.     }
  387.     
  388.     // URL
  389.     function switchproxy_anon_importUrl(){
  390.         var oField     = document.getElementById("import-url-field");
  391.         var oButton    = document.getElementById("import-url-button");
  392.         var oList    = document.getElementById("proxy-list");
  393.         var sValue    = oField.value;
  394.         
  395.         try{
  396.             
  397.             oButton.blur();
  398.             
  399.             //Cancel
  400.             if(oButton.label == "Cancel"){
  401.                 switchproxy_anon_cancelImport();
  402.                 oButton.label = switchproxy_getString("common.buttons.load");
  403.                 return;
  404.             }
  405.             
  406.             //Load
  407.             if(!switchproxy_isEmpty(sValue)){
  408.                 oButton.label    = switchproxy_getString("common.buttons.cancel");
  409.                 oField.value    = switchproxy_getString("toolbar.loading");
  410.                 
  411.                 switchproxy_anon_import("url", sValue, oList, null);
  412.                 
  413.                 oField.value    = sValue;
  414.                 oButton.label    = switchproxy_getString("common.buttons.load");
  415.                 
  416.                 if(switchproxy_anon_getNumImported() == 0)
  417.                     alert(switchproxy_getString("add.anon.import.url.validate.empty"));
  418.                 else if(switchproxy_anon_getNewImported() == 0)
  419.                     alert(switchproxy_getString("add.anon.import.validate.old"));
  420.                     
  421.                 gAutoTime = (new Date()).getTime()
  422.             }
  423.             
  424.             self.focus();
  425.             
  426.         }catch(err){ alert(switchproxy_getString("error.unknown") +"\n("+ err +")"); }
  427.     }
  428.  
  429.  
  430. /*
  431. * ENABLE FORM FIELDS
  432. */
  433.     // Enable Load button for the URL or File field
  434.     function switchproxy_anon_enableLoadButton(oField){
  435.         var oButton = null;
  436.         
  437.         if(oField.id == "import-url-field")
  438.             oButton = document.getElementById('import-url-button');
  439.         else if(oField.id == "import-file-field")
  440.             oButton = document.getElementById('import-file-button');
  441.         
  442.         if(oField && oButton && oField.value != ""){
  443.             oButton.disabled = false;
  444.         }
  445.         else if(oField && oButton && oField.value == ""){
  446.             oButton.disabled = true;
  447.         }
  448.     }
  449.     
  450.     // Toggles if the list is automatically generated from URL
  451.     //    if true, it disables the add button
  452.     function switchproxy_anon_enableAutoUrl(oCheckbox){
  453.         
  454.         var oAddField = document.getElementById('add-field');
  455.         var oAddButton = document.getElementById('add-button');
  456.         
  457.         if(oAddField == null || oAddButton == null)
  458.             return;
  459.         
  460.         if(oCheckbox.checked){
  461.             oAddField.value        = "";
  462.             oAddField.setAttribute("disabled", "true");
  463.             oAddButton.setAttribute("disabled", "true");
  464.         }
  465.         else{
  466.             oAddField.setAttribute("disabled", "false");
  467.             oAddButton.setAttribute("disabled", "false");
  468.         }
  469.     }
  470.     
  471.     // Enable Remove Field
  472.     function switchproxy_anon_enableRemove(doEnable){
  473.     
  474.         var oRemoveButton = document.getElementById('remove-button');
  475.             oRemoveButton.setAttribute("disabled", (!doEnable) );
  476.     }
  477.     
  478. /*
  479. * UTILS
  480. */
  481. function switchproxy_anon_addProxyToList(sProxy){
  482.     if(gProxyList != null){
  483.         
  484.         //Add
  485.         var oItem = gProxyList.appendItem(sProxy, sProxy);
  486.             oItem.setAttribute("onclick", "switchproxy_anon_enableRemove(true)");
  487.             oItem.setAttribute("style", "border-bottom: #CCCCCC 1px solid;");
  488.         
  489.         //Select Item
  490.         gProxyList.scrollToIndex(0);
  491.         if(gProxyList.selectedIndex > 0){
  492.             gProxyList.selectItem(oItem);
  493.         }
  494.     }
  495. }